package Engine.Utils;

/**
 * A 2d Vector implementation.
 * @author Lueko
 *
 */
public class Vector2f {

    private float x, y;
    
    public Vector2f(float x, float y) {
        this.x = x;
        this.y = y;
    }
    
    public float length() {
        return (float) Math.sqrt(x * x + y * y);
    }
    
    public float dot(Vector2f r) {
        return x * r.getX() + y * r.getY();
    }
    
    public Vector2f normalized() {
        float length = length();
        
        return new Vector2f(x / length, y / length);
    }
    
    public Vector2f rotate(float angle) {
        double rad = Math.toRadians(angle);
        double cos = Math.cos(rad);
        double sin = Math.sin(rad);
        
        return new Vector2f((float) (x * cos - y * sin), (float) (x * sin + y * cos));
    }
    
    public Vector2f add(Vector2f r) {
        return new Vector2f(x + r.getX(), y + r.getY());
    }
    
    public Vector2f add(float r) {
        return new Vector2f(x + r, y + r);
    }
    
    public Vector2f addX(float x) {
    	return new Vector2f(this.x + x, this.y);
    }
    
    public Vector2f addY(float y) {
    	return new Vector2f(this.x, this.y + y);
    }
    
    public Vector2f sub(Vector2f r) {
        return new Vector2f(x - r.getX(), y - r.getY());
    }
    
    public Vector2f sub(float r) {
        return new Vector2f(x - r, y - r);
    }
    
    public Vector2f mul(Vector2f r) {
        return new Vector2f(x * r.getX(), y * r.getY());
    }
    
    public Vector2f mul(float r) {
        return new Vector2f(x * r, y * r);
    }
    
    /**
     * Divides the vector by an other vector.
     * @param r The vector to devide by.
     * @return The divided vector.
     */
    public Vector2f div(Vector2f r) {
        return new Vector2f(x / r.getX(), y / r.getY());
    }
    
    /**
     * Divides the vector by a value.
     * @param r The dividend.
     * @return The divided vector.
     */
    public Vector2f div(float r) {
        return new Vector2f(x / r, y / r);
    }
    
    /**
     * @return The absolute Vector2f.
     */
    public Vector2f abs() {
        return new Vector2f(Math.abs(x), Math.abs(y));
    }
    
    /**
     * @return A string representation of the vector.
     */
    public String toString() {
        return "(" + x + " " + y + ")";
    }
    
    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }
    
    public void setVector(float x, float y) {
        this.x = x;
        this.y = y;
    }
    
    public void setVector(Vector2f vector) {
        this.x = vector.x;
        this.y = vector.y;
    }
    
}

